package com.thingple.usb.session;

import android.util.Log;

/**
 * 使session在线程中独立运行
 */
public abstract class SessionThread extends StreamProcessor {

    private static final String TAG = SessionThread.class.getName();

    Thread thread;

    @Override
    protected void init() {
        super.init();

        Log.d(TAG, "Init session " + device.getManufacturer() + "_" + device.getModel());
        onTrace("Init session " + device.getManufacturer() + "_" + device.getModel());

        thread = new Thread(() -> {
            Log.d(TAG, "Start Accessory Session");
            onTrace("Start Accessory Session");
            loop();
        }, "AccessorySession");
    }
    public void start() {
        if (work) {
            throw new IllegalStateException("重复启动过了");
        }
        work = true;
        if (thread != null) {
            thread.start();
        }
    }
    public void stop() {
        work = false;
    }
}
